define(['jquery','common'], function($,common){
    var listCheck = {
        //入口初始化
        onLoad:function(element){
            listCheck.onChange(element+' input[type="checkbox"]');
            listCheck.onSubmit(element+' button[type="button"]');
        },
        //监听多选状态
        onChange: function(element){
            $(document).on("change", element, function(){
                var inputId = $(this).parents(".xf-list-check").find('.dropdown-toggle').attr('id');
                listCheck.changeVal('#'+inputId);
                //alert($(this).prop('checked'));
            });
        },
        //修改input值
        changeVal: function(element){
            var checkbox = $(element).next('.dropdown-menu');
            var chkValue = [];
            $(checkbox).find("input[type='checkbox']:checked").each(function(){
                chkValue.push($(this).data('label'));
            });
            $(element).val(chkValue.join(','));
        },
        //AJAX查询
        onSubmit: function(element){
            $(document).on("click", element, function(){
                //表单字段
                var id = $(this).prev("input[type='text']");
                var value = $(id).val();
                if(!value){
                    $(id).attr('placeholder','不能为空').focus();
                    return false;
                }
                //是否重复
                var values = $(this).parents(".xf-list-check").find(".col label").text();
                if(values.includes(value)){
                    $(id).val('').attr('placeholder','重复了').focus();
                    return false;
                }
                //按钮提示
                var btnSubmit = $(this);
                var title = $(this).text();
                $(this).text('Loading');
                //AJAX上传
                $.ajax({
                    url: $(this).data('url'),
                    type: 'post',
                    cache: false,
                    data: {'name':value},
                    error: function(){
                        common.toast('加载失败',2000);
                    },
                    success: function(res,status,xhr){
                        //console.log(res);
                        if(res.code == 1){
                            listCheck.appendCheck(btnSubmit,res.data);
                        }else{
                            common.toast(res.msg,1000);
                        }
                    },
                    complete: function(xhr,status){
                        $(id).val('');
                        btnSubmit.text(title);
                    }
                });
            });
        },
        //增加一个checkbox
        appendCheck:function(element,json){
            var inputId = $(element).parents(".xf-list-check").find('.dropdown-toggle').attr('id');
            var checkId = inputId+Math.random();
            var html = '<div class="col small pt-1"><input class="form-check-input" type="checkbox" name="'+inputId+'[]" id="'+checkId+'" value="'+json.id+'" data-label="'+json.name+'" data-target="#'+inputId+'" checked> <label class="form-check-label" for="'+checkId+'">'+json.name+'</label></div>';
            $(element).parents('.col-12').before(html);
            listCheck.changeVal('#'+inputId);
            //var html = $(element).parents('.row').find('.col:last').prop('outerHTML');
        }
    };
    return listCheck;
});